﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
            var sql = "select * from Articles";
            var dt = DbHelper.Table(sql);
            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].HeaderCell.Value = "ID号";
            dataGridView1.Columns[1].HeaderCell.Value = "标题";
            dataGridView1.Columns[2].HeaderCell.Value = "作者";
            dataGridView1.Columns[3].HeaderCell.Value = "时间";
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//选择一行
            dataGridView1.AllowUserToAddRows = false;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql = string.Format("select * from Articles where Title like '%{0}%'", name);
            dataGridView1.DataSource= DbHelper.Table(sql);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            AddandModificationForm addandModificationForm = new AddandModificationForm();
            var res = addandModificationForm.ShowDialog();

            if (res == DialogResult.Yes)
            {
                var name = textBox1.Text;
                var sql = string.Format("select * from Articles");
                var dt = DbHelper.Table(sql);
                dataGridView1.DataSource = dt;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                //把记录里的值提取出来
                var id = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
                var Title = (string)dataGridView1.SelectedRows[0].Cells[1].Value;
                var Author = (string)dataGridView1.SelectedRows[0].Cells[2].Value;

                //使用析构函数
                AddandModificationForm addandModificationForm = new AddandModificationForm(id, Title, Author);
                var res = addandModificationForm.ShowDialog();
                if(res== DialogResult.Yes)
                {

                    var sql = "select * from Articles";
                    var dt = DbHelper.Table(sql);
                    dataGridView1.DataSource = dt;
                }
            }
            else
            {
                MessageBox.Show("请单击选择数据", "提示");
            }

            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //判断有没有选中数据
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];

                var Id = (int)row.Cells["Id"].Value;

                if (Id > 0)
                {
                    //弹出确认删除的对话框
                    var res = MessageBox.Show("确认删除吗", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                    //判断用户到底想不想删除

                    if (res.Equals(DialogResult.Yes))
                    {
                        var sql = string.Format("delete from Articles where Id={0}", Id);

                        var rowCount = DbHelper.Operate(sql);

                        if (rowCount > 0)
                        {
                            //显示数据源
                            var sql2 = "select * from Articles";
                            var dt = DbHelper.Table(sql2);
                            dataGridView1.DataSource = dt;
                        }
                        else
                        {
                            MessageBox.Show("删除失败", "提示");
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择数据", "提示");
            }
        }
    }
}
